var Sequelize = require('sequelize');
var conf = require('../config/config');
var sequelize = new Sequelize(conf.mysql.database, conf.mysql.user, conf.mysql.password, {
    host: conf.mysql.host,
    port: conf.mysql.port,
    dialect: 'mysql',
    pool: {
        maxConnections: 200,
        minConnections: 20,
        maxIdleTime: 2000
    },
    timezone: '+08:00',
    logging: false,
    maxConcurrentQueries: 100
});

var downMessage = sequelize.define('DownMessage', {
    content:Sequelize.STRING(1024),
    time:Sequelize.DATE,
    receiver:Sequelize.STRING,
    state:Sequelize.INTEGER(2),
},{
    tableName: 'DownMessage',
    timestamps: false
});

var uploadMessage = sequelize.define('UploadMessage', {
    content:Sequelize.STRING(1024),
    time:Sequelize.DATE,
    sender:Sequelize.STRING,
},{
    tableName: 'UploadMessage',
    timestamps: false
});

var uploadLocation = sequelize.define('UploadLocation',{
    sender:Sequelize.STRING, //发送方
    lon: Sequelize.FLOAT,   //经度
    lat: Sequelize.FLOAT,   //纬度
    alarm: Sequelize.STRING(1), //报警
    time: Sequelize.DATE    //时间
},{
    timestamps: false,
    tableName: 'UploadLocation'
});

var uploadLocationUniq = sequelize.define('UploadLocationUniq',{
    sender:Sequelize.STRING, //发送方
    lon: Sequelize.FLOAT,   //经度
    lat: Sequelize.FLOAT,   //纬度
    alarm: Sequelize.STRING(1), //报警
    time: Sequelize.DATE    //时间
},{
    timestamps: false,
    tableName: 'UploadLocationUniq'
});

sequelize.sync();

module.exports = {
    sequelize:sequelize,
    downMessage: downMessage,
    uploadMessage: uploadMessage,
    uploadLocation: uploadLocation,
    uploadLocationUniq: uploadLocationUniq
};